package com.xdf.xzymanagementsystem.controller;

import com.xdf.xzymanagementsystem.base.BaseController;
import com.xdf.xzymanagementsystem.dto.ExcelInputParamDTO;
import com.xdf.xzymanagementsystem.dto.JsonResult;
import com.xdf.xzymanagementsystem.dto.QueryParams;
import com.xdf.xzymanagementsystem.entity.JcShearingFactory;
import com.xdf.xzymanagementsystem.enums.MenuConstant;
import com.xdf.xzymanagementsystem.enums.OperateConstant;
import com.xdf.xzymanagementsystem.enums.ResponseEnum;
import com.xdf.xzymanagementsystem.enums.SeparatorConstant;
import com.xdf.xzymanagementsystem.service.JcShearingFactoryService;
import com.xdf.xzymanagementsystem.util.Example;
import com.xdf.xzymanagementsystem.util.ExcelUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.xdf.xzymanagementsystem.entity.ResponseResult;

import java.util.List;
import java.util.Map;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description : 自动生成的控制层
 * @Author : 张柯
 * @Date : 2020-12-31 14:42:38
 */
@Api(tags = "剪毛厂")
@CrossOrigin
@RestController
@RequestMapping("/jcShearingFactory")
public class JcShearingFactoryController extends BaseController {

    @Autowired
    private JcShearingFactoryService service;
    @Autowired
    private ExcelUtils excelUtils;

    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.ADD)
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<Void> insertSelective(JcShearingFactory record) throws Exception {
        service.insertSelective(record);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 批量新增
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "批量新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.ADD)
    @RequestMapping(value = "/multiInsert", method = RequestMethod.POST)
    public ResponseResult<Void> multiInsert(@RequestBody List<JcShearingFactory> list) throws Exception {
        service.multiInsert(list);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 选择修改
     * @Return : JcShearingFactory
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<Void> updateSelective(JcShearingFactory record) throws Exception {
        service.updateByPrimaryKeySelective(record);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 批量修改
     * @Return : JcShearingFactory
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "批量修改", httpMethod = "POST", notes = "自动生成的修改方法")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.ADD)
    @RequestMapping(value = "/multiUpdate", method = RequestMethod.POST)
    public ResponseResult<Void> multiUpdate(@RequestBody List<JcShearingFactory> list) throws Exception {
        service.multiUpdate(list);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 根据主键查询
     * @Return : JcShearingFactory
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByPk/{id}", method = RequestMethod.GET)
    public JcShearingFactory selectByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        if (id == null) return null;
        return service.selectByPrimaryKey(id);
    }

    /**
     * @Description : 根据编码查询
     * @Return : JcShearingFactory
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByCode/{code}", method = RequestMethod.GET)
    public JcShearingFactory selectByCode(@PathVariable("code") String code) throws Exception {
        return service.selectByCode(code);
    }

    /**
     * @Description : 根据主键查询所有收货地址
     * @Return : JcShearingFactory
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键查询所有收货地址", httpMethod = "GET", notes = "根据主键查询所有收货地址")
    @RequestMapping(value = "/queryAddressByPk/{id}", method = RequestMethod.GET)
    public List<Map<String, Object>> selectSendAddressById(@PathVariable("id") Integer id) {
        return service.selectSendAddressById(id);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<JcShearingFactory> query(JcShearingFactory record) throws Exception {
        return service.listQuery(record);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/jsonQuery", method = RequestMethod.GET)
    public JsonResult<JcShearingFactory> query(QueryParams params) throws Exception {
        if (params == null) return null;
        return JsonResult.<JcShearingFactory>builder()
                .rows(service.listQuery(new Example().setRowSize(params.getRows()).setStartRow(params.getStartRow()).setRowSize(params.getRows())))
                .total(service.count(new Example()))
                .build();
    }

    /**
     * @Description : 根据主键删除方法
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键删除方法", httpMethod = "DELETE", notes = "自动生成的删除方法")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.DELETE)
    @RequestMapping(value = "/deleteByPk", method = RequestMethod.POST)
    public ResponseResult<Void> deleteByPrimaryKey(@RequestParam("id") Integer id) throws Exception {
        service.deleteByPrimaryKey(id);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * 导入Excel文件，批量新增
     *
     * @param file    文件
     * @param request 参数
     * @return
     */
    @ApiOperation(value = "导入Excel文件,批量新增", httpMethod = "POST", notes = "导入Excel文件方法")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.ADD)
    @PostMapping("/upload")
    public ResponseResult<Void> uploadExcel(MultipartFile file, ExcelInputParamDTO param, HttpServletRequest request) throws Exception {
        Assert.notNull(file, "文件未上传!");
        String fileName = file.getOriginalFilename();
        Assert.state(isExcel(fileName), "不是Excel文件");
        if (param != null) {
            param.setFileName(fileName);
            param.setFieldSetting(conventToMap(request));
        }
        List<JcShearingFactory> list = excelUtils.readExcel(file.getInputStream(), param, JcShearingFactory.class);
        if (CollectionUtils.isNotEmpty(list)) {
            for (JcShearingFactory jcShearingFactory : list) {
                service.insertSelective(jcShearingFactory);
            }
        }
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    @GetMapping("/seq/{id}/{direction}")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    public ResponseResult<Void> moveClientSeq(@PathVariable int id, @PathVariable int direction){
        service.moveClientSeq(id, direction);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }
}